<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false" top="0" custom-class="dialog-right">
    <el-form size="small" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item label="租户ID" prop="tenantId">
        <el-input v-model="dataForm.tenantId" placeholder="租户ID"></el-input>
      </el-form-item>
      <el-form-item label="租户名称" prop="tenantName">
        <el-input v-model="dataForm.tenantName" placeholder="租户名称"></el-input>
      </el-form-item>
      <el-form-item label="域名地址" prop="domain">
        <el-input v-model="dataForm.domain" placeholder="域名地址"></el-input>
      </el-form-item>
      <el-form-item label="系统背景" prop="backgroundUrl">
        <el-input v-model="dataForm.backgroundUrl" placeholder="系统背景"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="linkman">
        <el-input v-model="dataForm.linkman" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="contactNumber">
        <el-input v-model="dataForm.contactNumber" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="联系地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="联系地址"></el-input>
      </el-form-item>
      <el-form-item label="账号额度" prop="accountNumber">
        <el-input v-model="dataForm.accountNumber" placeholder="账号额度"></el-input>
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
        <el-input v-model="dataForm.expireTime" placeholder="过期时间"></el-input>
      </el-form-item>
      <el-form-item label="数据源ID" prop="datasourceId">
        <el-input v-model="dataForm.datasourceId" placeholder="数据源ID"></el-input>
      </el-form-item>
      <el-form-item label="授权码" prop="licenseKey">
        <el-input v-model="dataForm.licenseKey" placeholder="授权码"></el-input>
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input v-model="dataForm.creator" placeholder="创建人"></el-input>
      </el-form-item>
      <el-form-item label="创建部门" prop="deptId">
        <el-input v-model="dataForm.deptId" placeholder="创建部门"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="修改人" prop="updater">
        <el-input v-model="dataForm.updater" placeholder="修改人"></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateDate">
        <el-input v-model="dataForm.updateDate" placeholder="修改时间"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-input v-model="dataForm.status" placeholder="状态"></el-input>
      </el-form-item>
      <el-form-item label="是否已删除" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否已删除"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button size="small" @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button size="small" type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        tenantId: '',
        tenantName: '',
        domain: '',
        backgroundUrl: '',
        linkman: '',
        contactNumber: '',
        address: '',
        accountNumber: '',
        expireTime: '',
        datasourceId: '',
        licenseKey: '',
        creator: '',
        deptId: '',
        createDate: '',
        updater: '',
        updateDate: '',
        status: '',
        isDeleted: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        tenantId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tenantName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        domain: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        backgroundUrl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        linkman: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        contactNumber: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        address: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        accountNumber: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        expireTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        datasourceId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        licenseKey: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        creator: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updater: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        isDeleted: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/sys/tenant/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/tenant/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style lang="scss">
</style>
